﻿package mortal.game.scene3D.ai.singleAIs.autofight
{
    import Message.Public.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.component.gconst.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.ai.ais.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.data.*;
    import mortal.game.scene3D.map3D.sceneInfo.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.util.*;
    import mortal.game.utils.*;
    import mortal.game.view.copy.petBreak.*;

    public class AutofightStrategyBase extends Object implements IAutofightStrategy
    {
        protected var _ai:AutoFightAI;
        protected var _waittingTime:int = 0;
        protected var _hasResetBossPoint:Boolean = false;
        protected var _minDisToCurBossPoint:int = 2147483647;
        public static var instance:AutofightStrategyBase = new AutofightStrategyBase;

        public function AutofightStrategyBase()
        {
            return;
        }// end function

        public function setParams(param1:AutoFightAI) : void
        {
            this._ai = param1;
            this._hasResetBossPoint = false;
            AutoFightBossSelectAI.instance.isOnlyInRange = false;
            AutoFightBossSelectAI.instance.rangRect = SceneRange.display;
            this._minDisToCurBossPoint = int.MAX_VALUE;
            return;
        }// end function

        public function chooseMovePath() : void
        {
            var _loc_1:Point = null;
            if (this._ai == null || this._ai.data == null)
            {
                return;
            }
            if (this._ai.data.range <= 0)
            {
                if (this.checkAndQuickCopy())
                {
                    return;
                }
                if (!this._hasResetBossPoint)
                {
                    this.resetToBossPoint();
                    this._hasResetBossPoint = true;
                }
                _loc_1 = Cache.instance.autoFight.getCurBossRereshPoint();
            }
            else
            {
                _loc_1 = AutoFightBossSelectAI.instance.sourcePoint;
            }
            if (_loc_1 == null)
            {
                return;
            }
            this._ai.cancelAll();
            this._ai.addMoveTo(_loc_1, 20);
            this._ai.startAIList();
            return;
        }// end function

        protected function resetToBossPoint() : void
        {
            Cache.instance.autoFight.resetToNearestBossPoint();
            return;
        }// end function

        protected function checkAndQuickCopy() : Boolean
        {
            var _loc_1:IEntity = null;
            var _loc_2:Array = null;
            var _loc_3:int = 0;
            var _loc_4:IEntity = null;
            if (!Cache.instance.autoFight.isLastBossRefreshPoint())
            {
                return false;
            }
            if (this._waittingTime > 0 && getTimer() - this._waittingTime < 1300)
            {
                return false;
            }
            if (Game.mapInfo != null && GameMapUtil.isCrossNormalMap(Game.mapInfo.mapId))
            {
                return false;
            }
            if (Cache.instance.copy.isInCopy)
            {
                if (CopyUtil.isInNotAutoOutCopy || CopyUtil.isInZZXYBattleCopy)
                {
                    return false;
                }
                _loc_2 = ThingUtil.entityUtil.entitysMap.getEntityByRangle(new Rectangle(), EEntityType._EEntityTypeBoss, false);
                _loc_2.sort(AutoFightBossSelectAI.instance.sortByDistance);
                _loc_3 = 0;
                while (_loc_3 < _loc_2.length)
                {
                    
                    _loc_4 = _loc_2[_loc_3];
                    if (_loc_4 == null || _loc_4.entityInfo == null || _loc_4.entityInfo.isDead)
                    {
                    }
                    else if (_loc_4.entityInfo.entityInfo.life <= 0)
                    {
                    }
                    else if (!FightUtil.isNormalAttackable(_loc_4, false))
                    {
                    }
                    else
                    {
                        _loc_1 = _loc_4;
                        break;
                    }
                    _loc_3++;
                }
                if (_loc_1 != null)
                {
                    return false;
                }
                this._waittingTime = getTimer();
                this._ai.cancelAll();
                this._ai.addDelayAI(1300);
                this._ai.addCallback(this.timeCheckToPass);
                this._ai.startAIList();
                return true;
            }
            return false;
        }// end function

        private function timeCheckToPass() : void
        {
            var _loc_5:SPassPoint = null;
            var _loc_6:SPassPoint = null;
            var _loc_10:int = 0;
            this._waittingTime = 0;
            if (AutoFightPickUpItemAI.hasDrop)
            {
                return;
            }
            var _loc_1:* = SceneConfig.instance.getSceneInfo(Game.sceneInfo.sMapDefine.mapId);
            var _loc_2:* = _loc_1.passInfos;
            if (_loc_2 == null || _loc_2.length == 0)
            {
                return;
            }
            var _loc_3:* = AutoFightBossSelectAI.instance.getEntity();
            if (_loc_3 != null)
            {
                return;
            }
            if (Cache.instance.role.roleInfo.level <= GameConst.LeaveCopyAutoTaskMaxLv && GameController.guide.resumeToTask())
            {
                return;
            }
            var _loc_4:* = _loc_2[0] as SPassPoint;
            var _loc_7:* = int.MAX_VALUE;
            var _loc_8:int = 0;
            while (_loc_8 < _loc_2.length)
            {
                
                _loc_4 = _loc_2[_loc_8];
                if (!ThingUtil.passUtil.isAddToStage(_loc_4))
                {
                }
                else
                {
                    _loc_6 = _loc_4;
                    if (_loc_4.passTo == null || _loc_4.passTo.length == 0 || (_loc_4.passTo[0] as SPassTo).toPoint == null)
                    {
                        _loc_10 = GeomUtil.calcDistance(_loc_4.point.x, _loc_4.point.y, RolePlayer.instance.x2d, RolePlayer.instance.y2d);
                        if (_loc_10 < _loc_7)
                        {
                            _loc_7 = _loc_10;
                            _loc_5 = _loc_4;
                        }
                    }
                }
                _loc_8++;
            }
            if (_loc_5 == null)
            {
                _loc_5 = _loc_6;
            }
            if (!_loc_5)
            {
                return;
            }
            var _loc_9:* = new Point();
            _loc_9.x = _loc_5.point.x;
            _loc_9.y = _loc_5.point.y;
            this._ai.cancelAll();
            this._ai.addMoveTo(_loc_9, 0);
            this._ai.addPassToMap(_loc_5);
            this._ai.startAIList();
            return;
        }// end function

        public function updateMinDisToCurBossPoint() : void
        {
            var _loc_1:* = Cache.instance.autoFight.getCurBossRereshPoint();
            if (_loc_1 == null)
            {
                return;
            }
            var _loc_2:* = GeomUtil.calcDistance(_loc_1.x, _loc_1.y, PetBreakCopyUtil.player.x2d, PetBreakCopyUtil.player.y2d);
            if (this._minDisToCurBossPoint > _loc_2)
            {
                this._minDisToCurBossPoint = _loc_2;
            }
            return;
        }// end function

        protected function updateToNextBossPoint() : void
        {
            this.updateMinDisToCurBossPoint();
            if (this._minDisToCurBossPoint <= 50)
            {
                this._minDisToCurBossPoint = int.MAX_VALUE;
                Cache.instance.autoFight.setToNextBossRefreshPoint();
            }
            return;
        }// end function

    }
}
